const _ = require('../utils')
const { listItemBehavior } = require('../behaviors')

_.create('checkbox', {
  properties: {
    label: String,
    value: null,
    disabled: Boolean,
  },

  behaviors: [listItemBehavior],

  relations: {
    '../checkbox-group/index': {
      type: 'parent',
    },
  },

  methods: {
    onTap(e) {
      if (this.data.disabled) return

      const detail = e.detail
      const { index, checked } = this.data
      detail.index = index
      detail.checked = !checked

      this.setData({ checked: !checked })

      this.triggerEvent('checked', detail, {
        bubbles: true,
        composed: true,
      })
    },

    setChecked(checked) {
      this.setData({ checked })
    },
  },
})
